package com.vanzo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.vanzo.bean.MembersListBeen;
import com.vanzo.encryption.R;
import com.vanzo.interfaces.OnRecyclerViewItemClickListener;

import java.util.List;

/**
 * Created by Administrator on 2018/3/5.
 */

public class MemberListRecyclerAdapter extends RecyclerView.Adapter<MemberListRecyclerAdapter.ViewHolder> implements View.OnClickListener {

    private final List<MembersListBeen> mMembersListBeenList;
    private OnRecyclerViewItemClickListener mOnItemClickListener;
    private final Context mContext;

    public MemberListRecyclerAdapter(Context context , List<MembersListBeen> membersListBeenList){
        mContext = context;
        mMembersListBeenList = membersListBeenList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = View.inflate(mContext, R.layout.member_list_item, null);
        inflate.setOnClickListener(this);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        if(holder == null){
            return;
        }

        holder.tMemberName.setText(mMembersListBeenList.get(position).getName());
        holder.mDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mOnItemClickListener != null){
                    mOnItemClickListener.onDelete(position);
                }
            }
        });
        holder.mMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mOnItemClickListener != null){
                    mOnItemClickListener.onMessage(position);
                }
            }
        });
        holder.mTalkie.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mOnItemClickListener != null){
                    mOnItemClickListener.onTalkie(position);
                }
            }
        });

        holder.itemView.setTag(position);
    }

    @Override
    public int getItemCount() {
        return mMembersListBeenList==null ? 0 : mMembersListBeenList.size();
    }

    @Override
    public void onClick(View v) {
        int tag = (int) v.getTag();
        if(mOnItemClickListener != null){
            mOnItemClickListener.onItemClick(v , tag);
        }
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


    class ViewHolder extends RecyclerView.ViewHolder{

        private TextView tMemberName;
        private ImageView mDelete;
        private ImageView mTalkie;
        private ImageView mMessage;

        public ViewHolder(View itemView) {
            super(itemView);
            tMemberName = (TextView) itemView.findViewById(R.id.tv_member_name);
            mDelete = (ImageView) itemView.findViewById(R.id.iv_delete);
            mTalkie = (ImageView) itemView.findViewById(R.id.iv_talkie);
            mMessage = (ImageView) itemView.findViewById(R.id.iv_message);
        }
    }
}
